package com.bestv.utils.config;

import com.bestv.utils.common.Reloadable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Properties;

/**
 * 全局设置
 * User: Henry
 * Date: 14-5-5
 */
public class GlobalSettings extends Reloadable {
    private Logger logger = LoggerFactory.getLogger(getClass());

    private Properties properties = new Properties();
    String configFile;
    String memcachedConfigFileURL;
    String recommenderConfigFile;

    private String dbDriver;
    private String dbUrl;
    private String masterDbUrl;
    private String dbUserName;
    private String dbPassword;

    private String recommendationEngineHome;
    private String outputDirectory;
    private String viewlogDirectory;
    private String loggingDirectory;
    private String indexDirectory;
    private String programCFTrainDataFolder;
    private String seriesCFTrainDataFolder;
    private String programSimilarityFolder;
    private String seriesSimilarityFolder;

    private String memcachedDataSyncInterval;

    private static GlobalSettings instance;

    public static synchronized GlobalSettings getInstance() {
        if (instance == null) {
            instance = new GlobalSettings();
        }
        return instance;
    }

    private GlobalSettings() {
        InputStream settingsInputStream = getClass().getResourceAsStream("/prop.properties");
        Properties settingsProperties = new Properties();

        try {
            settingsProperties.load(settingsInputStream);
            configFile = settingsProperties.getProperty("SETTINGS_FILE", "/home/bestv/conf/settings.properties");
            memcachedConfigFileURL = settingsProperties.getProperty("MEMCACHED_CONFIG_FILE_URL", "file:///home/bestv/conf/memcached.xml");
            recommenderConfigFile  = settingsProperties.getProperty("RECOMMENDER_CONFIG_FILE",   "/home/bestv/conf/recommender_config.xml");
            reload();
            setConfigFile(configFile);
            start();
        } catch (IOException e) {
            logger.error("Error on loading global settings.", e);
        }

    }

    @Override
    protected void reload() {
        try {
            InputStream configInputStream = new FileInputStream(configFile);
            InputStreamReader configReader = new InputStreamReader(configInputStream, "UTF-8");
            properties.load(configReader);

            dbDriver = properties.getProperty("dbdriver", "com.mysql.jdbc.Driver");
            dbUrl = properties.getProperty("dburl", "");
            masterDbUrl = properties.getProperty("masterdburl", "");
            dbUserName = properties.getProperty("dbusername", "root");
            dbPassword = properties.getProperty("dbpassword", "mysql");

            recommendationEngineHome = properties.getProperty("recommendationenginehome", "/cmsdata");
            outputDirectory          = properties.getProperty("outputdirectory",          "output");
            viewlogDirectory         = properties.getProperty("viewlogdirectory",         "viewlog");
            loggingDirectory         = properties.getProperty("loggingdirectory",         "logging");
            indexDirectory           = properties.getProperty("indexdirectory",           "luceneindex");
            programCFTrainDataFolder = properties.getProperty("programcftraindatafolder", "cf/cf_program");
            seriesCFTrainDataFolder  = properties.getProperty("seriescftraindatafolder",  "cf/cf_series");
            programSimilarityFolder  = properties.getProperty("programsimilarityfolder",  "similarity/similarity_program");
            seriesSimilarityFolder   = properties.getProperty("seriessimilarityfolder",   "similarity/similarity_series");
            memcachedDataSyncInterval= properties.getProperty("memcachedDataSyncInterval", "3600");
        } catch (IOException e) {
            logger.error("Error on loading global settings.", e);
        }
    }

    public void list() {
        properties.list(System.out);
    }

    public String getMemcachedConfigFileURL() {
        return memcachedConfigFileURL;
    }

    public String getRecommenderConfigFile() {
        return recommenderConfigFile;
    }

    public String getDbDriver() {
        return dbDriver;
    }

    public String getDbUrl() {
        return dbUrl;
    }

    public String getMasterDbUrl() {
        return masterDbUrl;
    }

    public String getDbUserName() {
        return dbUserName;
    }

    public String getDbPassword() {
        return dbPassword;
    }

    public String getOutputDirectory() {
        return combine(recommendationEngineHome, outputDirectory);
    }

    public String getViewlogDirectory() {
        return combine(recommendationEngineHome, viewlogDirectory);
    }

    public String getLoggingDirectory() {
        return combine(recommendationEngineHome, loggingDirectory);
    }

    public String getIndexDirectory() {
        return combine(recommendationEngineHome, indexDirectory);
    }

    public String getProgramCFTrainDataFolder() {
        return combine(recommendationEngineHome, programCFTrainDataFolder);
    }

    public String getSeriesCFTrainDataFolder() {
        return combine(recommendationEngineHome, seriesCFTrainDataFolder);
    }

    public String getProgramSimilarityFolder() {
        return combine(recommendationEngineHome, programSimilarityFolder);
    }

    public String getSeriesSimilarityFolder() {
        return combine(recommendationEngineHome, seriesSimilarityFolder);
    }

    private String combine(String base, String file) {
        File path = new File(base, file);
        return path.getPath();
    }

    public int getMemcachedDataSyncInterval() {
        return Integer.parseInt(memcachedDataSyncInterval);
    }
}
